package fuyongkang;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Random;
import java.util.Scanner;
import java.util.Set;

public class Fuyongkang {


    public class DoubleColorBall {
        public static void main(String[] args) {
            // 生成中奖号码
            int[] winningNumbers = generateWinningNumbers();
            // 获取用户输入号码
            int[] userNumbers = getUserInputNumbers();

            // 统计命中红球个数
            int redBallHitCount = countHitBalls(winningNumbers, userNumbers, 0, 5);
            // 统计命中蓝球个数
            int blueBallHitCount = countHitBalls(winningNumbers, userNumbers, 6, 6);

            System.out.println("中奖号码:");
            printNumbers(winningNumbers);
            System.out.println("您投注号码为:");
            printNumbers(userNumbers);
            System.out.println("您命中了" + redBallHitCount + "个红色球号码, " + blueBallHitCount + "个蓝色球号码。");

            // 判断中奖等级
            String prizeLevel = determinePrizeLevel(redBallHitCount, blueBallHitCount);
            System.out.println(prizeLevel);
        }

        // 生成中奖号码
        private static int[] generateWinningNumbers() {
            Random random = new Random();
            Set<Integer> redBallSet = new HashSet<>();
            while (redBallSet.size() < 6) {
                redBallSet.add(random.nextInt(33) + 1);
            }
            int[] numbers = new int[7];
            int index = 0;
            for (int num : redBallSet) {
                numbers[index++] = num;
            }
            numbers[6] = random.nextInt(16) + 1;
            return numbers;
        }

        // 获取用户输入号码
        private static int[] getUserInputNumbers() {
            Scanner scanner = new Scanner(System.in);
            Set<Integer> redBallSet = new HashSet<>();
            int[] numbers = new int[7];

            for (int i = 0; i < 6; i++) {
                while (true) {
                    System.out.print("请输入红球号码，输入任意一个范围1-33的数字：");
                    int num = scanner.nextInt();
                    if (num >= 1 && num <= 33 && redBallSet.add(num)) {
                        numbers[i] = num;
                        break;
                    } else {
                        System.out.println("号码无效或已重复，请重新输入！");
                    }
                }
            }

            while (true) {
                System.out.print("请输入蓝球号码，输入任意一个范围1-16的数字：");
                int num = scanner.nextInt();
                if (num >= 1 && num <= 16) {
                    numbers[6] = num;
                    break;
                } else {
                    System.out.println("号码无效，请重新输入！");
                }
            }

            return numbers;
        }

        // 统计命中球个数
        private static int countHitBalls(int[] winningNumbers, int[] userNumbers, int startIndex, int endIndex) {
            int count = 0;
            for (int i = startIndex; i <= endIndex; i++) {
                for (int j = 0; j < 7; j++) {
                    if (winningNumbers[i] == userNumbers[j]) {
                        count++;
                        break;
                    }
                }
            }
            return count;
        }

        // 打印号码
        private static void printNumbers(int[] numbers) {
            List<Integer> redBalls = new ArrayList<>();
            for (int i = 0; i < 6; i++) {
                redBalls.add(numbers[i]);
            }
            int blueBall = numbers[6];
            System.out.println(redBalls + ", " + blueBall);
        }

        // 判断中奖等级
        private static String determinePrizeLevel(int redBallHitCount, int blueBallHitCount) {
            if (redBallHitCount == 6 && blueBallHitCount == 1) {
                return "一等奖，最高1000万";
            } else if (redBallHitCount == 6 && blueBallHitCount == 0) {
                return "二等奖，最高500万";
            } else if (redBallHitCount == 5 && blueBallHitCount == 1) {
                return "三等奖，3000元";
            } else if (redBallHitCount == 5 && blueBallHitCount == 0 || redBallHitCount == 4 && blueBallHitCount == 1) {
                return "四等奖，200元";
            } else if (redBallHitCount == 4 && blueBallHitCount == 0 || redBallHitCount == 3 && blueBallHitCount == 1) {
                return "五等奖，10元";
            } else if (redBallHitCount == 2 && blueBallHitCount == 1 || redBallHitCount == 1 && blueBallHitCount == 1 || redBallHitCount == 0 && blueBallHitCount == 1) {
                return "六等奖，5元";
            } else {
                return "很遗憾，你没中奖";
            }
        }
    }
}